<?php

include_once '..\clases\ConexionBD.php';
include_once '..\utils\ConstantesGlobales.php';

session_start();
session_unset();

if(isset($_POST['ingresar']))
{

    $usuario = $_POST['usuario'];
    $password = $_POST['password'];

    $conexionBD = new ConexionBD();
    $conexionBD->abrirConexion();

    $consulta = "SELECT user, password FROM usuario WHERE user='".$usuario."'";

    $namecheck = mysql_query($consulta,$conexionBD->getConexion());

    $rowValues = Array();
    while ($linea = mysql_fetch_array($namecheck, MYSQL_ASSOC)) {
        foreach ($linea as $valor_col) {
            $rowValues[] = $valor_col;
        }
    }

        if (md5($password) == $rowValues[1] && $rowValues[0] == $usuario) {
            $namecheck = mysql_query($GLOBALS['consultaListaCiudadesDepartamentos'],$conexionBD->getConexion());
            $codigo = "";
            while($row = mysql_fetch_array($namecheck)) {
                        $codigo = $codigo . '<option value="'.$row[0].'","'.$row[1].'">'.$row[0].','.$row[1].'</option>';
            } 
           $_SESSION['conexionBD'] = serialize($conexionBD);
           $_SESSION['listaHTMLCiudadDepa'] = $codigo;

           if(isset($_SESSION['mensajeLogin']))

           session_write_close();

           header('Location: ../principal.php');
           exit();

        } else {

            session_unset();

            session_write_close();

            header('Location: ../login.php?mensajeLogin=error');
            exit();
        }
}elseif(isset($_POST['registrar']))
{
    header('Location: ../formularios/FormularioSignUp.php');
    exit();
}
?>
